package org.edu.modules.sxjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.entity.OssFile;
import org.edu.modules.sxjy.entity.VeDoc;
import org.edu.modules.sxjy.mapper.VeDocMapper;
import org.edu.modules.sxjy.oss.FileDTO;
import org.edu.modules.sxjy.service.IOssFileService;
import org.edu.modules.sxjy.service.IVeDocService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeDocVo;
import org.edu.modules.sxjy.vo.VeDocVos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeDocServiceImpl
        extends ServiceImpl<VeDocMapper, VeDoc>
        implements IVeDocService
{
    @Autowired
    private VeDocMapper veDocMapper;
    @Autowired
    private IVeDocService veDocService;
    @Autowired
    private IOssFileService ossFileService;

    public List<VeDocVos> selectCateCode(VeDocVos veDocVos, Integer startLine, Integer pageSize)
    {
        List<VeDocVos> veDocVosList = this.veDocMapper.selectCateCode(veDocVos, startLine, pageSize);
        for (VeDocVos docVos : veDocVosList) {
            docVos.setCreateTime(DateUtils.getDateToString(Long.parseLong(docVos.getCreateTime())));
        }
        return veDocVosList;
    }

    public int getSumPage(VeDocVos veDocVos)
    {
        return this.veDocMapper.getSumPage(veDocVos);
    }

    public VeDocVo editEcho(int id)
    {
        VeDoc veDoc = (VeDoc)this.veDocService.getById(Integer.valueOf(id));

        VeDocVo veDocVo = new VeDocVo();


        veDocVo.setCateCode(veDoc.getCateCode());

        veDocVo.setTitle(veDoc.getTitle());

        veDocVo.setContent(veDoc.getContent());
        if ((veDoc.getFileId() != null) && (!veDoc.getFileId().equals("0")))
        {
            FileDTO fileDTO = new FileDTO();

            fileDTO.setWebUrl(((OssFile)this.ossFileService.getById(veDoc.getFileId())).getUrl());

            String url = ((OssFile)this.ossFileService.getById(veDoc.getFileId())).getUrl();
            fileDTO.setOldFileName(url.substring(url.lastIndexOf("-") + 1));
            veDocVo.setFileDTO(fileDTO);
        }
        return veDocVo;
    }

    public void saves(VeDocVo veDocVo)
    {
        VeDoc veDoc = new VeDoc();


        veDoc.setCateCode(veDocVo.getCateCode());



        veDoc.setTitle(veDocVo.getTitle());

        veDoc.setContent(veDocVo.getContent());

        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        veDoc.setCreateUserId(user.getId());

        veDoc.setCreateUserName(user.getUsername());

        veDoc.setCreateTime(System.currentTimeMillis());

        veDoc.setUpdateTime(System.currentTimeMillis());
        if (veDocVo.getFileDTO() != null)
        {
            String webUrl = veDocVo.getFileDTO().getWebUrl();
            OssFile ossFile = new OssFile();

            String url = veDocVo.getFileDTO().getWebUrl();
            ossFile.setFileName(url.substring(url.lastIndexOf("-") + 1));

            ossFile.setUrl(webUrl);

            ossFile.setCreateBy(user.getRealname());

            ossFile.setCreateTime(new Date());

            ossFile.setUpdateBy(user.getRealname());

            ossFile.setUpdateTime(new Date());

            this.ossFileService.save(ossFile);


            List<String> ossFileIdList = this.veDocMapper.getOssFileIdByUrl(ossFile.getUrl());
            if ((ossFileIdList != null) && (ossFileIdList.size() > 0)) {
                veDoc.setFileId((String)ossFileIdList.get(ossFileIdList.size() - 1));
            }
        }
        veDoc.setTerminalId(1);


        this.veDocService.save(veDoc);
    }

    public void edit(VeDocVo veDocVo)
    {
        VeDoc veDoc = (VeDoc)this.veDocService.getById(Integer.valueOf(veDocVo.getId()));

        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();

        veDoc.setTitle(veDocVo.getTitle());

        veDoc.setContent(veDocVo.getContent());
        if (veDocVo.getFileDTO() != null)
        {
            String webUrl = veDocVo.getFileDTO().getWebUrl();
            OssFile ossFile = new OssFile();

            String url = veDocVo.getFileDTO().getWebUrl();
            ossFile.setFileName(url.substring(url.lastIndexOf("-") + 1));

            ossFile.setUrl(webUrl);

            ossFile.setCreateBy(loginUser.getRealname());

            ossFile.setCreateTime(new Date());

            ossFile.setUpdateBy(loginUser.getRealname());

            ossFile.setUpdateTime(new Date());

            this.ossFileService.save(ossFile);


            List<String> ossFileIdList = this.veDocMapper.getOssFileIdByUrl(ossFile.getUrl());
            if ((ossFileIdList != null) && (ossFileIdList.size() > 0)) {
                veDoc.setFileId((String)ossFileIdList.get(ossFileIdList.size() - 1));
            }
        }
        this.veDocService.updateById(veDoc);
    }
}
